package cn.itcast.dewu.controller;

import cn.itcast.dewu.dto.Result;
import cn.itcast.dewu.pojo.Blog;
import cn.itcast.dewu.pojo.User;
import cn.itcast.dewu.service.BlogService;
import cn.itcast.dewu.service.UserService;
import cn.itcast.dewu.utils.RedisIdWork;
import cn.itcast.dewu.utils.UserHolder;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blog")
public class BolgController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisIdWork redisIdWork;

    /*
     *  新增播客
     */
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        return blogService.saveBlog(blog);
    }

    /*
     * 播客点赞
     * 参数 当前播客的id*/
    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        // 修改点赞数量
        return blogService.likeBlog(id);
    }

    /*
     * 查看自己的播客
     * 参数 当前页,默认为1
     * */
    @GetMapping("/of/me")
    public Result queryMyBolg(@RequestParam(value = "corrent", defaultValue = "1") Integer corrent) {
        //获取当前用户
        Long userid = UserHolder.getUser().getId();
        Page<Blog> page = blogService.query().eq("user_id", userid)
                .page(new Page<Blog>(corrent, 10));
        //获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    /* 查看所有的播客
     * 参数当前页
     * */
    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryHotBlog(current);
    }

    /*
     *查看一个播客的详细信息
     * 参数 当前播客id
     * */
    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id) {
        return blogService.queryBlogById(id);
    }

    /*
     * 查看播客点赞的所有人
     * 参数 播客id
     * */
    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id) {
        return blogService.queryBlogLikes(id);
    }

    /*
     * 删除某个播客
     * */
    @DeleteMapping("/{id}")
    public Result deleteBlog(@PathVariable("id") Long id) {
        return blogService.deleteBlog(id);
    }

    /*
     * 推送播客给粉丝
     * */
    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(@RequestParam("max") Long max
            , @RequestParam(value = "offset"
            , required = false, defaultValue = "0") Integer offset) {

        return blogService.queryBlogOfFollow(max, offset);
    }

    /*
     * 输入内容进行查询
     * */

    @GetMapping("/query/blogOrGoods/{corrent}")
    public Result queryblogOrGoods(@PathVariable("corrent")Integer corrent
            ,@RequestParam("content") String content) {
        return blogService.queryblogByContent(corrent,content);
    }
}
